﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Entities;

namespace InsuranceVehicle
{
    public partial class UpdateCustomerInfo : System.Web.UI.Page
    {

        CustomerDataAccess db = new CustomerDataAccess();
        protected void Page_Load(object sender, EventArgs e)
        {
            CustomerInfo c = new CustomerInfo();
            if (!IsPostBack)
            {
                if (Session["CustomerID"] == null)
                {
                    panelFalse.Visible = true;
                    panelUpdate.Visible = false;
                    panelUpdatePass.Visible = false;
                    lblNotLogin.Text = "You are not Logined";

                }
                else
                {
                    panelUpdate.Visible = true;
                    panelFalse.Visible = false;
                    panelUpdatePass.Visible = false;
                    addListItem();
                    c = db.getCustomerById(Convert.ToInt32(Session["CustomerID"].ToString()));

                    txtFirstname.Text = c.Firstname.ToString();
                    txtLastname.Text = c.Lastname.ToString();
                    txtPersonalIdentity.Text = c.PersonalIdentity.ToString();
                    txtAccount.Text = c.Account.ToString();
                    txtJob.Text = c.Job.ToString();
                    string dob = c.Dob.ToString();
                    string[] strArr = dob.Split('/');

                    cbbMonth.SelectedValue = strArr[0];
                    cbbDay.SelectedValue = strArr[1];
                    cbbYear.SelectedValue = strArr[2];

                    txtAddress.Text = c.Address.ToString();
                    txtPhone.Text = c.Phone.ToString();
                    txtEmail.Text = c.Email.ToString();
                }
            }


        }

        private void addListItem()
        {
            for (int i = 1; i <= 12; i++)
            {
                cbbMonth.Items.Add(i.ToString());
            }

            for (int i = 1; i <= 31; i++)
            {
                cbbDay.Items.Add(i.ToString());
            }

            int curYear = DateTime.Now.Year;
            for (int i = curYear - 80; i <= curYear - 18; i++)
            {
                cbbYear.Items.Add(i.ToString());
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            CustomerInfo c = new CustomerInfo();
            int custID = Convert.ToInt32(Session["CustomerID"].ToString());
            if (IsValid)
            {

                c.CustomerID = custID;
                c.Firstname = txtFirstname.Text;
                c.Lastname = txtLastname.Text;

                c.Job = txtJob.Text;
                c.Dob = cbbMonth.SelectedItem.ToString() + "/" +
                    cbbDay.SelectedItem.ToString() + "/" + cbbYear.SelectedItem.ToString();
                c.Address = txtAddress.Text;
                c.Phone = txtPhone.Text;
                c.Email = txtEmail.Text;

                db.UpdateInfomation(c);

                Response.Write("<script>alert('Update successfull')</script>");
            }
        }

        protected void btnUpdatePass_Click(object sender, EventArgs e)
        {
            int custID = Convert.ToInt32(Session["CustomerID"].ToString());
            string oldPass = txtOlbPassword.Text;
            string newPass = txtNewPasword.Text;
            string conPass = txtConPass.Text;
            bool flagOldPass;
            bool flagNewPass = false;

            if (oldPass.Equals(""))
            {
                Response.Write("<script>alert('Old password is empty')</script>");
                //lblErrOldPass.Text = "Old password is empty";
                txtOlbPassword.Focus();
                flagOldPass = false;
            }
            else
            {
                bool confirmPass = db.ConfirmPassword(custID, oldPass);
                if (confirmPass == false)
                {
                    Response.Write("<script>alert('Old password is wrong')</script>");
                    txtOlbPassword.Focus();
                    flagOldPass = false;
                }
                else
                {
                    flagOldPass = true;
                    if (newPass.Equals(""))
                    {
                        Response.Write("<script>alert('New password is empty')</script>");

                        txtNewPasword.Focus();
                        flagNewPass = false;
                    }
                    else if (!newPass.Equals(conPass))
                    {
                        Response.Write("<script>alert('New password and confirm password is not match')</script>");

                        flagNewPass = false;

                    }
                    else
                    {
                        flagNewPass = true;
                    }
                }

            }


            if (flagNewPass == true && flagOldPass == true)
            {
                db.UpdatePassword(custID, newPass);
                Response.Write("<script>alert('Update password successful')</script>");
            }

        }

        protected void linkPass_Click(object sender, EventArgs e)
        {
            panelUpdatePass.Visible = true;
            panelUpdate.Visible = false;
        }

        protected void linkInfo_Click(object sender, EventArgs e)
        {
            panelUpdatePass.Visible = false;
            panelUpdate.Visible = true;
        }

    }
}